package com.vaadin.tutorial.crm.ui;

import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.tutorial.crm.ui.view.dashboard.DashboardView;
import com.vaadin.tutorial.crm.ui.view.list.*;
import com.vaadin.tutorial.crm.ui.view.upload.UploadView;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.component.tabs.TabVariant;
import com.vaadin.flow.router.HighlightConditions;
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.theme.lumo.Lumo;
import com.vaadin.tutorial.crm.backend.util.CommonComponent;

//import com.vaadin.tutorial.crm.ui.view.list.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 主界面
 * @Author: fan
 * @DateTime: 2021-01-15 9:03
 **/
//@CssImport("./styles/shared-styles.css")
@CssImport("./styles/views/main/main-view.css")
@PWA(name = "学生管理系统", shortName = "学生管理系统",  offlineResources = {"./styles/offline.css", "./images/offline.png"}) //安装到桌面或者APP需要用到该注解
// 可设置明亮或者黑暗主题
@Theme(value = Lumo.class, variant = Lumo.LIGHT)
// AppLayout是具有标题和响应式的Vaadin布局（标题和抽屉式的侧栏）
public class MainLayout extends AppLayout implements AppShellConfigurator {
    public static final String TITLE_LOGOUT = "登出";

    public MainLayout() {
        createHeader();
        createDrawer();
    }

    private void createHeader() {
        H3 logo = new H3("学生管理系统");
        logo.addClassName("logo");

        //获取当前登录用户
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        /*HtmlComponent div = new HtmlComponent(Tag.DIV);
        div.getElement().setText("欢迎：" + auth.getName());*/
        Button button_user = CommonComponent.createButton(ButtonVariant.LUMO_TERTIARY, auth.getName(), VaadinIcon.USER, false);
        button_user.getStyle().set("right", "60px");

        Button button_logout = CommonComponent.createButton(ButtonVariant.LUMO_ICON, "登出", VaadinIcon.ARROW_RIGHT, true);
        button_logout.getStyle().set("right", "10px");
        Anchor logout = new Anchor("login"); //创建一个链接，链接到/logout
        logout.add(button_logout);

        /*String contextPath = VaadinServlet.getCurrent().getServletContext().getContextPath();
        Tab logoutTab = CommonComponent.createTab(createLogoutLink(contextPath), TabVariant.LUMO_ICON_ON_TOP);*/

        //DrawerToggle是一个菜单按钮，用于切换侧栏
        HorizontalLayout header = new HorizontalLayout(new DrawerToggle(), logo, button_user);
        //让logo占据布局中所有额外的空间,这将注销按钮推到最右边
        header.expand(logo);
        //标题垂直居中
        header.setDefaultVerticalComponentAlignment(
                FlexComponent.Alignment.CENTER);
        header.setWidth("100%");
        header.addClassName("header");

        //将标题布局添加到应用布局的顶部导航栏中
        this.addToNavbar(header,logout);
    }

    private void createDrawer() {
        //侧栏导航
        RouterLink dashboardViewLink = new RouterLink("统计信息", DashboardView.class);
        RouterLink stuListLink = new RouterLink("学生管理", StudentView.class);
        RouterLink scoreListLink = new RouterLink("成绩管理", ScoreView.class);
        RouterLink courseListLink = new RouterLink("课程管理", CourseView.class);
        RouterLink teacherListLink = new RouterLink("老师管理", TeacherView.class);
        RouterLink userListLink = new RouterLink("用户管理", UserView.class);

        RouterLink uploadLink = new RouterLink("上传组件", UploadView.class);
        stuListLink.setHighlightCondition(HighlightConditions.sameLocation());

        //将List跳转链接包装在VerticalLayout中，并将其添加到AppLayout的侧栏中
        addToDrawer(new VerticalLayout(
                dashboardViewLink, stuListLink, courseListLink, teacherListLink,
                scoreListLink, userListLink));
    }

    private static Tab createTab(Component content) {
        final Tab tab = new Tab();
        tab.addThemeVariants(TabVariant.LUMO_ICON_ON_TOP);
        tab.add(content);
        return tab;
    }

    private static Anchor createLogoutLink(String contextPath) {
        final Anchor a = populateLink(new Anchor(), VaadinIcon.ARROW_RIGHT, TITLE_LOGOUT);
        a.setHref(contextPath + "/logout");
        return a;
    }

    private static <T extends HasComponents> T populateLink(T a, VaadinIcon icon, String title) {
        a.add(icon.create());
        a.add(title);
        return a;
    }

}
